package Server;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import Server.Impl.*;
import VWorld.Admin;
import VWorld.AdminHelper;
import VWorld.AvatarManager;
import VWorld.AvatarManagerHelper;
import VWorld.RoomManager;
import VWorld.RoomManagerHelper;

public class VWorldServer {
	public static ORB orb;
	public static void main(String args[]) throws InvalidName{
		try {
			orb = ORB.init(args, null);
			POA rootpoa = POAHelper.narrow (orb.resolve_initial_references("RootPOA")); 
			rootpoa.the_POAManager().activate();
			
			AvatarManagerImpl avatarManagerImpl = new AvatarManagerImpl(); 	
			avatarManagerImpl.setORB(orb); 
			
			AdminImpl adminImpl = new AdminImpl(); 	
			adminImpl.setORB(orb); 
			
			RoomManagerImpl roomManagerImpl = new RoomManagerImpl(); 	
			roomManagerImpl.setORB(orb); 
			

			org.omg.CORBA.Object avatarManagerRef = rootpoa.servant_to_reference(avatarManagerImpl); 
			AvatarManager avatarManager = AvatarManagerHelper.narrow(avatarManagerRef); 
			
			org.omg.CORBA.Object adminRef = rootpoa.servant_to_reference(adminImpl); 
			Admin admin = AdminHelper.narrow(adminRef); 

			org.omg.CORBA.Object roomManagerRef = rootpoa.servant_to_reference(roomManagerImpl); 
			RoomManager roomManager = RoomManagerHelper.narrow(roomManagerRef); 
			
			

			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); 

			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); 
			
			NameComponent path1[] = ncRef.to_name( "Avatar" ); 
			ncRef.rebind(path1, avatarManager); 
			
			NameComponent path2[] = ncRef.to_name( "Room" ); 
			ncRef.rebind(path2, roomManager); 
			
			NameComponent path3[] = ncRef.to_name( "Admin" ); 
			ncRef.rebind(path3, admin); 

			System.out.println("VWorld Server ready and waiting ..."); 
			orb.run(); 
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
}